Libraries

The following libraries were used:

library(tidyverse)
library(here)
library(plotly)
library(htmlwidgets)
library(lubridate)

Adding size data

load(file = here("data/data_tidy.RData")) # load object data_tidy

BodyLengths <- tibble(Species = c("Ceriodaphnia sp.", "C. megalops", "D. cucullata", "D. curvirostris", "D. hyalina var. gellata", "D. hyalina var. lacustris", "D. hyalina", "D. longispina", "D. pulex", "D. magna", "Calanoid copepods", "Cyclops", "Bosmina coregoni", "B. longirostris", "Sida sp.", "S. crystallina", "Chydorus ovalis", "Eurycercus lamellatus", "Alona spp.", "A. quadrangularis", "Asplanchna", "Keratella spp.", "Ostracod", "Diaptomus"),
                      BodyLength = c(0.4636, 0.925, 0.8593, 1.58, 0.9064, 0.9064, 0.9064, 1.0347, 1.1656, 1.4214, 0.6860, 1.0369, 0.4230, 0.3637, 0.5075, 0.5075, 0.475, 1.975, 0.5235, 0.9679, 0.4747, 0.3495, 1.25, 0.9886)) # species and their body sizes

data <- left_join(data_tidy, BodyLengths, by = "Species") # add species body size to the dataframe

data <- mutate(data, SizeClass = case_when(BodyLength <= 0.6 ~ "Small (<= 0.6 mm)",
                                           BodyLength <= 1.0 ~ "Medium ( 0.6 < x <= 1.0 mm)",
                                           BodyLength > 1.0 ~ "Large (> 1.0 mm)")) # define different categories of body sizes

Data wrangling and creating plots

Using nested for loops, R cycles through all six lakes and year by year. It then creates plots of all possible combinations.

lakes <- unique(data_tidy$LakeName) # extract the names of the lakes
years <- unique(data_tidy$Year) # extract the years where counts were measured

plotlist <- list() # create an empty list to add to

for (i in 1:length(lakes)){
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) {
      
      Xdata <- Ydata %>% mutate("Abundance_ind/L" = (Counts / Proportion_of_sample_counted) / Sampling_volume_net_L,
                                "Relative_abundance" = Counts / Total_individuals * 100)
      
      summary <- Xdata %>% group_by(Species, SizeClass, Date) %>% summarize(sum_counts = sum(Counts, na.rm = TRUE),
                                                                             sum_total = sum(Total_individuals, na.rm = TRUE)) # Calculate counts per Date
      
      summary <- summary %>% group_by(SizeClass, Date, sum_total) %>% summarize(sum_counts = sum(sum_counts, na.rm = TRUE))
      
      summary <- summary %>% mutate("Relative_abundance" = sum_counts / sum_total * 100) # Calculate relative abundance per Date
      result <- summary %>% ggplot(aes(x = factor(Date), y = Relative_abundance, fill = SizeClass))+
        geom_col(color = "Black", linewidth = 0.05)+
        labs(x = "Month", y = "Relative abundance (%)", title = paste0(lakes[[i]], " in ", years[[j]]))+
        scale_x_discrete(labels = month(summary$Date, label = TRUE))+
        theme_minimal()
      
      plotlist[[(length(plotlist)+1)]] <- result
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]]))
    }
  }
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012

Interactive plots

The {plotly} package was used to create interactive plots, to make it possible to see the actual values of a bar.

htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))

Creating plots for abundance ind/L

plotlist <- list()
lakes <- unique(data$LakeName)
years <- unique(data$Year)

for (i in 1:length(lakes)){
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) {
      
      summary <- Ydata %>% group_by(SizeClass, Date) %>% summarize(CountsClass = sum(Counts, na.rm = TRUE))
      Zdata <- unique(Ydata[, c("Sampling_volume_net_L", "Proportion_of_sample_counted", "Date")])
      Xdata <- left_join(summary, Zdata, by = "Date")
      Pdata <- Xdata %>% mutate("Abundance_ind/L" = (CountsClass / Proportion_of_sample_counted) / Sampling_volume_net_L)
      
      result <- Pdata %>% ggplot(aes(x = factor(Date), y = `Abundance_ind/L`, fill = SizeClass))+
        geom_col(color = "Black", linewidth = 0.05)+
        labs(x = "Month", y = "Abundance ind/L", title = paste0(lakes[[i]], " in ", years[[j]]))+
        scale_x_discrete(labels = month(Pdata$Date, label = TRUE))+
        theme_minimal()
      
      plotlist[[(length(plotlist)+1)]] <- result
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]]))
    }
  }
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012
htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))
for (i in 1:length(lakes)){
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) {
      
      summary <- Ydata %>% group_by(Species, Date) %>% summarize(sum_counts = sum(Counts, na.rm = TRUE),
                                                                            sum_total = sum(Total_individuals, na.rm = TRUE))
      summary <- summary %>% mutate("Relative_abundance" = sum_counts / sum_total * 100) # Calculate relative abundance per Date
      
      summaryW <- left_join(summary, BodyLengths, by = "Species")
      
      WeightedMean <- summaryW %>% group_by(Date) %>% summarize(WeightedMean = weighted.mean(BodyLength, Relative_abundance))
      
      print(knitr::kable(WeightedMean, 
             format = "markdown",
             caption = paste0("The weighted mean for ", lakes[i], " in ", years[j])))
      
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]]))
    }
  }
}
The weighted mean for Coneries Lake in 2005
Date WeightedMean
2005-03-22 0.6894216
2005-04-15 0.6782098
2005-05-12 0.8266748
2005-06-09 0.8363835
2005-07-06 0.6994522
2005-08-01 0.6808251
2005-08-31 0.6503360
2005-10-04 0.5875464
2005-11-01 0.5390154
2005-11-24 0.4790086
2005-12-21 0.6009585
The weighted mean for Coneries Lake in 2006
Date WeightedMean
2006-01-17 0.6533893
2006-02-16 0.6859954
2006-03-15 0.6711121
2006-04-20 0.6848271
2006-05-18 0.8441469
2006-06-15 0.8204418
2006-07-13 0.5954968
2006-08-10 0.6069626
2006-09-07 0.4516269
2006-10-05 0.4546698
2006-11-02 0.6818792
2006-11-30 0.8668976
2006-12-20 0.8453357
The weighted mean for Coneries Lake in 2007
Date WeightedMean
2007-01-24 0.7360909
2007-02-15 0.6718000
2007-03-15 0.6650282
2007-04-19 0.7680665
2007-05-17 0.8628428
2007-06-14 0.5683685
2007-07-16 0.4620341
2007-08-08 0.3862955
2007-09-11 0.5124218
2007-10-15 0.4650030
2007-11-15 0.6437393
2007-12-13 0.6424000
The weighted mean for Coneries Lake in 2008
Date WeightedMean
2008-01-17 0.6442160
2008-02-19 0.7762519
2008-03-18 0.7092926
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
The weighted mean for Tween Pond in 2005
Date WeightedMean
2005-03-22 0.6667670
2005-04-15 0.5794297
2005-05-12 0.7965538
2005-06-09 0.8218269
2005-07-06 0.6835896
2005-08-01 0.6786700
2005-08-31 0.6598628
2005-10-04 0.6118205
2005-11-01 0.4814944
2005-11-24 0.4758491
2005-12-21 0.5387302
The weighted mean for Tween Pond in 2006
Date WeightedMean
2006-01-17 0.4668333
2006-02-16 0.6822807
2006-03-15 0.5859857
2006-04-20 0.6490297
2006-05-18 0.7894466
2006-06-15 0.7551322
2006-07-13 0.6726927
2006-08-10 0.6514676
2006-10-15 0.4967880
2006-11-02 0.7928010
2006-11-30 0.6733050
2006-12-20 0.6362571
The weighted mean for Tween Pond in 2007
Date WeightedMean
2007-01-24 0.9064000
2007-02-15 0.7741600
2007-03-15 0.5510950
2007-04-19 0.7107042
2007-05-17 0.5833202
2007-06-14 0.6523470
2007-07-16 0.6132821
2007-08-08 0.4656252
2007-09-11 0.5966712
2007-10-15 0.5444653
2007-11-15 0.5134844
2007-12-13 0.5545000
The weighted mean for Tween Pond in 2008
Date WeightedMean
2008-01-17 0.5357143
2008-02-19 0.5282000
2008-03-18 0.6150833
The weighted mean for Tween Pond in 2010
Date WeightedMean
2010-01-21 0.9749500
2010-02-17 0.9716500
2010-03-24 1.0113571
2010-04-22 0.9275000
2010-05-17 0.7168524
2010-06-16 0.9186824
2010-07-21 0.7722008
2010-08-16 0.9617754
2010-09-21 0.9756113
2010-10-26 1.0484250
2010-11-23 0.9232545
The weighted mean for Tween Pond in 2011
Date WeightedMean
2011-01-20 0.9331000
2011-02-17 NA
2011-03-21 0.9544652
2011-04-14 1.0381762
2011-05-12 1.1199259
2011-06-09 1.0707315
2011-07-07 1.0368640
2011-08-04 1.0373408
2011-09-01 0.7277215
2011-09-29 0.5582440
2011-10-27 0.8360900
2011-11-25 1.1571804
2011-12-15 1.1344692
The weighted mean for Tween Pond in 2012
Date WeightedMean
2012-01-20 1.1559955
2012-02-16 1.1237725
2012-03-15 0.9123318
The weighted mean for Main Pond in 2005
Date WeightedMean
2005-03-22 0.7658733
2005-04-15 0.6522075
2005-05-12 0.7513805
2005-06-09 0.8773874
2005-07-06 0.7149213
2005-08-01 0.6792316
2005-08-31 0.6725702
2005-10-04 0.6783043
2005-11-01 0.4775421
2005-11-24 0.4515870
2005-12-21 0.4926794
The weighted mean for Main Pond in 2006
Date WeightedMean
2006-01-17 0.6403917
2006-02-16 0.6557250
2006-03-15 0.7068806
2006-04-20 0.5225646
2006-05-18 0.7311047
2006-06-15 0.7887271
2006-08-10 0.6541693
2006-09-07 0.6271550
2006-10-05 0.5287570
2006-11-02 0.6860000
2006-11-30 0.8448783
2006-12-20 0.9064000
The weighted mean for Main Pond in 2007
Date WeightedMean
2007-01-24 0.6975750
2007-02-15 0.7594667
2007-03-15 0.6571571
2007-04-19 0.6917827
2007-05-17 0.5785422
2007-06-16 0.6264174
2007-07-16 0.4500581
2007-08-08 0.4699650
2007-09-11 0.5704444
2007-10-15 0.4527506
2007-11-15 0.4805399
2007-12-13 0.4953412
The weighted mean for Main Pond in 2008
Date WeightedMean
2008-01-17 0.4814444
2008-02-19 0.6060714
2008-03-18 0.6167846
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
The weighted mean for Church Pond in 2005
Date WeightedMean
2005-03-22 0.8742286
2005-04-15 0.7437736
2005-05-12 0.8789709
2005-06-09 1.0404500
2005-07-06 1.1149833
2005-08-01 1.2497333
2005-08-31 0.9695286
2005-10-04 1.1888774
2005-11-01 1.0245200
2005-11-24 1.0504271
2005-12-21 1.0115095
The weighted mean for Church Pond in 2006
Date WeightedMean
2006-01-17 1.1520644
2006-02-16 0.9771634
2006-03-15 1.0962676
2006-04-20 NA
2006-05-18 0.8168472
2006-07-13 0.9607583
2006-08-10 0.7463900
2006-09-07 0.6879125
2006-10-05 0.5982887
2006-11-02 0.5962554
2006-11-30 0.6244777
2006-12-20 0.8089264
The weighted mean for Church Pond in 2007
Date WeightedMean
2007-01-24 0.6908571
2007-02-15 0.7922748
2007-03-15 0.7322819
2007-04-19 0.7734793
2007-05-17 0.6793540
2007-06-14 0.7461200
2007-07-16 1.3095440
2007-08-08 0.9983919
2007-09-11 0.5390860
2007-10-15 0.8513015
2007-11-15 0.7428648
2007-12-13 0.7021111
The weighted mean for Church Pond in 2008
Date WeightedMean
2008-01-17 0.9013712
2008-02-19 0.8418333
2008-03-18 0.8955890
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
The weighted mean for Clifton Pond in 2005
Date WeightedMean
2005-04-15 0.9590763
2005-05-12 1.0094000
2005-06-09 1.1354111
2005-07-06 0.8598218
2005-08-01 1.2988333
2005-08-31 0.5561544
2005-10-04 0.5364261
2005-11-01 0.9096667
2005-11-24 0.9349569
2005-12-21 0.9897207
The weighted mean for Clifton Pond in 2006
Date WeightedMean
2006-01-17 1.0127881
2006-02-16 0.9542943
2006-03-15 0.7785025
2006-04-19 0.7356063
2006-05-18 0.8591990
2006-06-15 0.9249500
2006-07-13 1.3617282
2006-08-10 0.8021402
2006-09-07 0.8359106
2006-10-05 0.9942676
2006-11-02 1.3080511
2006-11-30 1.3421566
2006-12-20 1.2718839
The weighted mean for Clifton Pond in 2007
Date WeightedMean
2007-01-24 1.3875184
2007-02-15 1.1985051
2007-03-15 0.8960098
2007-04-19 0.8803709
2007-05-17 0.7592120
2007-06-14 0.8599082
2007-07-16 1.2130696
2007-08-08 0.5262842
2007-09-11 0.4940495
2007-10-15 1.1561877
2007-11-15 1.2375500
2007-12-13 1.3830313
The weighted mean for Clifton Pond in 2008
Date WeightedMean
2008-01-17 1.0739793
2008-02-19 0.6860000
2008-03-18 0.7432073
The weighted mean for Clifton Pond in 2010
Date WeightedMean
2010-01-21 1.1227000
2010-02-17 1.0369000
2010-04-22 1.0369000
2010-05-17 1.0389500
2010-06-16 0.9978000
2010-07-21 0.8974300
2010-08-16 0.5766529
2010-09-21 0.9639916
2010-10-26 0.9589333
2010-11-23 1.0870115
The weighted mean for Clifton Pond in 2011
Date WeightedMean
2011-01-20 1.0711326
2011-02-17 1.1028847
2011-03-21 1.1080661
2011-04-14 1.0341234
2011-05-12 1.0081437
2011-06-09 1.1026874
2011-07-07 0.9723422
2011-08-04 1.0315333
2011-09-01 0.7475106
2011-09-29 0.8984364
2011-10-27 1.0008344
2011-11-25 0.9709103
2011-12-15 1.0789632
The weighted mean for Clifton Pond in 2012
Date WeightedMean
2012-01-20 1.140267
2012-02-16 0.988600
2012-03-15 1.074887
The weighted mean for Beeston Pond in 2005
Date WeightedMean
2005-04-15 0.7216591
2005-05-12 0.7220569
2005-06-09 0.6590311
2005-07-06 0.6799317
2005-08-01 0.4147607
2005-08-31 0.6059846
2005-10-04 0.6770057
2005-11-01 0.7253145
2005-11-24 0.7856394
2005-12-21 0.7665578
The weighted mean for Beeston Pond in 2006
Date WeightedMean
2006-01-17 0.7001398
2006-02-16 0.7302664
2006-03-15 0.7087491
2006-04-20 0.7987270
2006-05-18 0.7531363
2006-06-15 0.8243721
2006-07-13 0.5909568
2006-08-10 0.5402019
2006-09-07 0.6943156
2006-10-05 0.5687263
2006-11-02 0.8353661
2006-11-30 0.8243050
The weighted mean for Beeston Pond in 2007
Date WeightedMean
2007-01-24 1.4118729
2007-02-15 0.9301602
2007-03-15 0.7169408
2007-04-19 0.5391566
2007-05-17 0.9092448
2007-06-14 0.6756980
2007-07-16 0.5967098
2007-08-08 0.5530080
2007-09-11 0.6106745
2007-10-15 0.5586890
2007-11-15 0.7363064
2007-12-13 0.8362360
The weighted mean for Beeston Pond in 2008
Date WeightedMean
2008-01-17 0.8358921
2008-02-19 0.6917247
2008-03-18 0.6599460
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012